#include "planlist.h"
#include <QtGui>

Planlist::Planlist(QWidget *parent)
    : QWidget(parent)
{
    QLabel *TimeLabel = new QLabel(tr("Time:"));
    finishDateEdit = new QDateEdit();
    finishDateEdit->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    finishDateEdit->setDisplayFormat(QString("yyyy.MM.dd"));
    finishDateEdit->setDate(QDate::currentDate());

    QLabel *PlanLabel = new QLabel(tr("Plan: "));
    planEdit = new QLineEdit();

    resetButton = new QPushButton(tr("Reset"));
    submitButton = new QPushButton(tr("Submit"));
    saveButton = new QPushButton(tr("Save"));
    loadButton = new QPushButton(tr("Load"));
    delButton = new QPushButton(tr("Delete"));

    planlist = new QTableWidget();
    planlist->setColumnCount(3);
    QStringList *headers = new QStringList();
    (*headers)<<"FinishTime"<<"Plan"<<"AddTime";
    planlist->setHorizontalHeaderLabels(*headers);
    delete(headers);
    planlist->setEditTriggers(QAbstractItemView::NoEditTriggers);

    connect(resetButton,SIGNAL(clicked()),this,SLOT(reset()));
    connect(submitButton,SIGNAL(clicked()),this,SLOT(submit()));
    connect(saveButton,SIGNAL(clicked()),this,SLOT(savetofile()));
    connect(loadButton,SIGNAL(clicked()),this,SLOT(loadfromfile()));
    connect(delButton,SIGNAL(clicked()),this,SLOT(del()));

    QVBoxLayout *rightlayout = new QVBoxLayout;
    rightlayout->addWidget(resetButton);
    rightlayout->addWidget(submitButton);
    rightlayout->addWidget(delButton);
    rightlayout->addWidget(saveButton);
    rightlayout->addWidget(loadButton);

    QHBoxLayout *upperlayout = new QHBoxLayout;
    upperlayout->addWidget(TimeLabel);
    upperlayout->addWidget(finishDateEdit);

    QHBoxLayout *lowerlayout = new QHBoxLayout;
    lowerlayout->addWidget(PlanLabel);
    lowerlayout->addWidget(planEdit);

    QVBoxLayout *leftlayout = new QVBoxLayout;
    leftlayout->addLayout(upperlayout);
    leftlayout->addLayout(lowerlayout);
    leftlayout->addWidget(planlist);

    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addLayout(leftlayout);
    mainlayout->addLayout(rightlayout);

    setLayout(mainlayout);

}

void Planlist::reset()
{
    finishDateEdit->setDate(QDate::currentDate());
    planEdit->clear();
}

void Planlist::resetAll()
{
    planlist->setRowCount(0);
    content.clear();
    reset();
}

void Planlist::submit()
{
    int i,size=content.size();
    for(i=0; i<size ;i++)
        if (content.at(i).finishTime > finishDateEdit->date())
            break;
    addNewItem(QDate::currentDate(),finishDateEdit->date(),planEdit->text(),i);
}

void Planlist::addNewItem(QDate aT,QDate fT,QString pl)
{
    addNewItem(aT,fT,pl,planlist->rowCount());

}

void Planlist::addNewItem(QDate aT, QDate fT, QString pl,const int row)
{
    content.insert(row,PlanItem(aT,fT,pl));
    planlist->insertRow(row);
    planlist->setItem(row,0,new QTableWidgetItem(fT.toString(QString("yyyy.MM.dd"))));
    planlist->setItem(row,1,new QTableWidgetItem(pl));
    planlist->setItem(row,2,new QTableWidgetItem(aT.toString(QString("yyyy.MM.dd"))));

}

void Planlist::del()
{
    QModelIndexList indexlist = planlist->selectionModel()->selectedRows();
    QModelIndex index;
    QVector<int> todel;
    foreach(index,indexlist)
        todel.push_back(index.row());
    if (todel.size())
    {
        int row ,i=0;
        foreach(row,todel)
        {
            planlist->removeRow(row-i);
            content.erase(content.begin()+row-i);
            i++;
        }
    }
    else
        QMessageBox::information(this,tr("Error!"),tr("Please select a row."));
}

void Planlist::savetofile()
{
    QString fileName = QFileDialog::getSaveFileName(this,
        tr("Save Planlist"), "",
        tr("Planlist (*.plt);;All Files (*)"));

    if (fileName.isEmpty())
        return;
    else {
        QFile file(fileName);

        if (!file.open(QIODevice::WriteOnly)) {
            QMessageBox::information(this, tr("Unable to open file"),
                file.errorString());
            return;
        }

        QDataStream out(&file);
        out.setVersion(QDataStream::Qt_4_7);
        for (QVector<PlanItem>::iterator it = content.begin() ;
             it != content.end() ; it++)
                out << it->addTime << it->finishTime << it->plan ;
    }
}

void Planlist::loadfromfile()
{
    QString fileName = QFileDialog::getOpenFileName(this,
        tr("Open Planlist"), "",
        tr("Planlist (*.plt);;All Files (*)"));

    if (fileName.isEmpty())
        return;
    else {
        QFile file(fileName);

        if (!file.open(QIODevice::ReadOnly)) {
            QMessageBox::information(this, tr("Unable to open file"),
                file.errorString());
            return;
        }

        QDataStream in(&file);
        in.setVersion(QDataStream::Qt_4_7);
        resetAll();
        PlanItem temp;
        while(!in.atEnd())
        {
            in >> temp.addTime;
            in >> temp.finishTime;
            in >> temp.plan;
            addNewItem(temp.addTime,temp.finishTime,temp.plan);
        }
    }

}

Planlist::~Planlist()
{

}
